package com.alibaba.algo.apiimpl.user;

import com.alibaba.algo.cooperation.usercomponent.service.user.SysUserEmployeeService;
import com.alibaba.algo.cooperation.usercomponent.vo.req.user.SelectUserEmployeeByEmployeeIdRequestVO;
import com.alibaba.algo.cooperation.usercomponent.vo.res.user.SelectUserEmployeeByEmployeeIdResponseVO;
import com.alibaba.algo.core.base.YesOrNo;
import com.alibaba.algo.domain.user.SysUserEmployeeDomain;
import com.alibaba.algo.entity.user.SysUserEmployee;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 类SysUserEmployeeServiceImpl.java的实现描述：用户员工表 serviceimpl
 *
 * @author nieganggang 2021-03-12 16:08:17
 */
@Slf4j
@RestController
public class SysUserEmployeeServiceImpl implements SysUserEmployeeService {

    @Autowired
    private SysUserEmployeeDomain sysUserEmployeeDomain;

    /**
     * 删除用户和员工的关联关系
     *
     * @param userId 用户标识
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void deleteByUserId(Long userId) {
        SysUserEmployee sysUserEmployeeCon = new SysUserEmployee();
        sysUserEmployeeCon.setUserId(userId);
        List<SysUserEmployee> sysUserEmployeeList = sysUserEmployeeDomain.listByPara(sysUserEmployeeCon, new LinkedHashMap<>());
        for (SysUserEmployee sysUserEmployee : sysUserEmployeeList) {
            sysUserEmployee.setIsDeleted(YesOrNo.Y.name());
            sysUserEmployee.setGmtModified(LocalDateTime.now());
            if(null != sysUserEmployee.getId() && sysUserEmployee.getId() > 0){
                sysUserEmployeeDomain.updateById(sysUserEmployee);
            }
        }
    }

    @Override
    public void insert(Long userId, Long employeeId) {
        SysUserEmployee sysUserEmployee = new SysUserEmployee();
        sysUserEmployee.setUserId(userId);
        sysUserEmployee.setEmployeeId(employeeId);
        sysUserEmployeeDomain.insert(sysUserEmployee);
    }

    @Override
    public void updateIsDeletedByUserIdLastModified(Long userId, LocalDateTime lastModifyTime) {
        SysUserEmployee sysUserEmployee = new SysUserEmployee();
        sysUserEmployee.setUserId(userId);
        sysUserEmployee.setIsDeleted(YesOrNo.N.name());
        sysUserEmployee.setGmtModified(LocalDateTime.now());
        sysUserEmployeeDomain.updateSysUserEmployeeByUserIdLastModified(sysUserEmployee,lastModifyTime);
    }

    @Override
    public void update(Long userId, Long employeeId) {
        SysUserEmployee sysUserEmployeeCon = new SysUserEmployee();
        sysUserEmployeeCon.setUserId(userId);
        List<SysUserEmployee> sysUserEmployeeList = sysUserEmployeeDomain.listByPara(sysUserEmployeeCon, new LinkedHashMap<>());
        boolean delFlag = true;
        for (SysUserEmployee sysUserEmployee : sysUserEmployeeList) {
            Long empId = sysUserEmployee.getEmployeeId();
            if (empId.equals(employeeId)) {
                delFlag = false;
                break;
            }
        }
        if (delFlag) {
            for (SysUserEmployee sysUserEmployee : sysUserEmployeeList) {
                sysUserEmployee.setIsDeleted(YesOrNo.Y.name());
                sysUserEmployeeDomain.updateById(sysUserEmployee);
            }
            SysUserEmployee userEmployee = new SysUserEmployee();
            userEmployee.setUserId(userId);
            userEmployee.setEmployeeId(employeeId);
            sysUserEmployeeDomain.insert(userEmployee);
        }
    }

    @Override
    public List<SelectUserEmployeeByEmployeeIdResponseVO> selectListByEmployeeId(SelectUserEmployeeByEmployeeIdRequestVO vo) {
        SysUserEmployee sysUserEmployeeCon = new SysUserEmployee();
        sysUserEmployeeCon.setEmployeeId(vo.getEmployeeId());
        List<SysUserEmployee> sysUserEmployeeList = sysUserEmployeeDomain.listByPara(sysUserEmployeeCon, new LinkedHashMap<>());
        List<SelectUserEmployeeByEmployeeIdResponseVO> list = new ArrayList<>();
        for (SysUserEmployee sysUserEmployee : sysUserEmployeeList) {
            SelectUserEmployeeByEmployeeIdResponseVO vvo = new SelectUserEmployeeByEmployeeIdResponseVO();
            vvo.setEmployeeId(vo.getEmployeeId());
            vvo.setUserId(sysUserEmployee.getUserId());
            list.add(vvo);
        }
        return list;
    }

    @Override
    public List<SelectUserEmployeeByEmployeeIdResponseVO> selectListByUserId(SelectUserEmployeeByEmployeeIdRequestVO vo) {
        SysUserEmployee sysUserEmployeeCon = new SysUserEmployee();
        sysUserEmployeeCon.setUserId(vo.getUserId());
        List<SysUserEmployee> sysUserEmployeeList = sysUserEmployeeDomain.listByPara(sysUserEmployeeCon, new LinkedHashMap<>());
        List<SelectUserEmployeeByEmployeeIdResponseVO> list = new ArrayList<>();
        for (SysUserEmployee sysUserEmployee : sysUserEmployeeList) {
            SelectUserEmployeeByEmployeeIdResponseVO vvo = new SelectUserEmployeeByEmployeeIdResponseVO();
            vvo.setEmployeeId(vo.getEmployeeId());
            vvo.setUserId(sysUserEmployee.getUserId());
            list.add(vvo);
        }
        return list;
    }

}
